<?php
/**
 * Created by PhpStorm.
 * User: yf
 * Date: 2018/5/28
 * Time: 下午6:33
 */

namespace EasySwoole\EasySwoole;



use App\Storage\ChatMessage;
use App\Storage\OnlineUser;
use App\Utility\Mongodb\Driver;
use App\Utility\Mongodb\MongoClient;
use App\Utility\Pool\MysqlPool;
use App\Utility\Pool\RedisPool;
use App\WebSocket\WebSocketEvents;
use App\WebSocket\WebSocketParser;
use EasySwoole\Component\Pool\PoolManager;
use EasySwoole\EasySwoole\Swoole\EventRegister;
use EasySwoole\EasySwoole\AbstractInterface\Event;
use EasySwoole\FastCache\Cache;
use EasySwoole\Http\Request;
use EasySwoole\Http\Response;
use EasySwoole\Socket\Dispatcher;
use swoole_server;
use swoole_websocket_frame;

class EasySwooleEvent implements Event
{

    public static function initialize()
    {
        date_default_timezone_set('Asia/Shanghai');
    }

    /**
     * @param EventRegister $register
     * @throws \EasySwoole\Component\Pool\Exception\PoolException
     * @throws \EasySwoole\Component\Pool\Exception\PoolObjectNumError
     */
    public static function mainServerCreate(EventRegister $register)
    {

        $mysqlConf = PoolManager::getInstance()->register(MysqlPool::class, Config::getInstance()->getConf('MYSQL.POOL_MAX_NUM'));
        if ($mysqlConf === null) {
            //当返回null时,代表注册失败,无法进行再次的配置修改
            //注册失败不一定要抛出异常,因为内部实现了自动注册,不需要注册也能使用
            throw new \Exception('注册失败!');
        }
        //设置其他参数
        $mysqlConf->setMaxObjectNum(10)->setMinObjectNum(3);
        //reids联接池
        $redisConf=PoolManager::getInstance()->register(RedisPool::class, Config::getInstance()->getConf('REDIS.POOL_MAX_NUM'));
        $redisConf->setMaxObjectNum(20)->setMinObjectNum(5);

        //新建时间任务协程
        //ServerManager::getInstance()->getSwooleServer()->addProcess((new ProcessTimer())->getProcess());

        //注入Rpc
//        $rpcs = new RpcServer();
//        $rpcs->test();

        //--------------------------websocket---------------------------
//        $server = ServerManager::getInstance()->getSwooleServer();
//        OnlineUser::getInstance();
//        ChatMessage::getInstance();
//        Cache::getInstance()->setTempDir(EASYSWOOLE_ROOT . '/Temp')->attachToServer($server);
//
//        // 注册服务事件
//        $register->add(EventRegister::onOpen, [WebSocketEvents::class, 'onOpen']);
//        $register->add(EventRegister::onClose, [WebSocketEvents::class, 'onClose']);
//
//        // 收到用户消息时处理
//        $conf = new \EasySwoole\Socket\Config;
//        $conf->setType($conf::WEB_SOCKET);
//        $conf->setParser(new WebSocketParser);
//        $dispatch = new Dispatcher($conf);
//        $register->set(EventRegister::onMessage, function (swoole_server $server, swoole_websocket_frame $frame) use ($dispatch) {
//            $dispatch->dispatch($server, $frame->data, $frame);
//        });

        //加入mongodb联接
        //MongoClient::getInstance(new Driver())->attachServer(ServerManager::getInstance()->getSwooleServer());
    }

    public static function onRequest(Request $request, Response $response): bool
    {
        return true;
    }

    public static function afterRequest(Request $request, Response $response): void
    {

    }
}